﻿using KMeansAlgorithm;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utility;

namespace LearningWorkbench
{
    public partial class ClusteringForm : Form
    {
        private Thread _clusteringThread;
        public bool ClusterComplete { get; set; }
        public KMeans KMeans { get; set; }

        public ClusteringForm()
        {
            InitializeComponent();
        }

        private void ClusteringForm_Load(object sender, EventArgs e)
        {
            _clusteringThread = new Thread(compute);
        }

        private void compute()
        {
            var k = int.Parse(textBox1.Text);
            var d = WorkbenchModel.PrepInputFieldNames.Count;
            var centroids = new double[k][];
            Random r = new Random();

            for (var i = 0; i < k; ++i)
            {
                centroids[i] = new double[d];
                
                for (var j = 0; j < d; ++j)
                {
                    centroids[i][j] = r.NextDouble() * 0.6 - 0.3;
                }
            }

            KMeans = new KMeans(k, d, centroids);

            for (int i = 0; i < WorkbenchModel.Inputs.Count; ++i)
            {
                KMeans.AddData(WorkbenchModel.Inputs[i], WorkbenchModel.Ids[i]);
            }
  
            WorkbenchModel.ClusterResult = KMeans.Cluster();
            Console.WriteLine("Clustering complete!");
            MessageBox.Show("Clustering complete!");

            ClusterComplete = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != null)
            {
                try
                {
                    _clusteringThread.Start();
                }
                catch (Exception)
                {
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (ClusterComplete)
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Clustering is still processing..");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (ClusterComplete)
            {
                var clusters = new List<double[][]>();
                clusters.Add(WorkbenchModel.ClusterResult.Centroids);
                FileManager<double[][]>.Save("clusters.json", clusters);
                MessageBox.Show("clusters.json has been saved");
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Clustering is not done yet");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var centroids = FileManager<double[][]>.Load("clusters.json")[0];

            WorkbenchModel.ClusterResult = new KMeansResult()
            {
                Centroids = centroids
            };

            MessageBox.Show("clusters.json has been loaded");

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
